<?php
//------------------------------------------------------------ Contraintes d'accès de la page

	// on vérifie que cette page est appelée à partir de l'index et que l'utilisateur a le droit de consultation
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){
	
		// l'identifiant de la donnée à supprimer est indiqué par un entier
		if(isset($_GET['id']) && numericInt($_GET['id'])){
		
			$nb_listes = valueExists((int)$_GET['id'], "CODELISTE", "liste_plantes", $connexion);
			
			if($nb_listes == 1){ // la liste de plantes à afficher existe et son identifiant est unique
	
?>
<?php
/********************************* Règles :

	- id doit désigner une seule liste de plantes
	- La liste de plantes doit exister dans la BDD

*****************************************/

	$liste = array(); // on créé un tableau contenant toutes les informations du liste de plantes

	// récupération des informations du liste de plantes
	$query_recuperation_liste_plantes = "SELECT lp.NOMLISTEPLANTES AS nom, t.CODETAXON AS idTaxon, f.NOMFAMILLE AS famille, g.NOMGENRE AS genre, e.NOMESPECE AS espece, sesp.NOMSOUSESPECE AS sousespece, v.NOMVARIETE AS variete, sa.CODESACHETGRAINE as idSachet, sm.CODESEMIS as idSemis, tou.CODETOUFFE as idTouffe, sa.REFERENCESACHETGRAINES as refSachet, sm.REFERENCESEMIS as refSemis, tou.REFERENCETOUFFE as refTouffe ";
	$query_recuperation_liste_plantes .= "FROM liste_plantes lp ";
	$query_recuperation_liste_plantes .= "LEFT JOIN sachetgraines_est_dans_liste sgL INNER JOIN sachet_graines sa ON sgL.CODESACHETGRAINE = sa.CODESACHETGRAINE ON sgL.CODELISTE = lp.CODELISTE ";
  	$query_recuperation_liste_plantes .= "LEFT JOIN semis_est_dans_liste smL INNER JOIN semis sm ON smL.CODESEMIS = sm.CODESEMIS ON smL.CODELISTE = lp.CODELISTE ";
  	$query_recuperation_liste_plantes .= "LEFT JOIN touffe_est_dans_liste touL INNER JOIN touffe tou ON touL.CODETOUFFE = tou.CODETOUFFE ON touL.CODELISTE = lp.CODELISTE ";
	$query_recuperation_liste_plantes .= "INNER JOIN taxon t ON t.CODETAXON = sa.CODETAXON OR t.CODETAXON = sm.CODETAXON OR t.CODETAXON = tou.CODETAXON ";
	$query_recuperation_liste_plantes .= "LEFT JOIN est_de_famille l1 INNER JOIN famille f ON f.CODEFAMILLE = l1.CODEFAMILLE ON l1.CODETAXON = t.CODETAXON AND l1.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_liste_plantes .= "LEFT JOIN appartient_a_genre l2 INNER JOIN genre g ON g.CODEGENRE = l2.CODEGENRE ON l2.CODETAXON = t.CODETAXON AND l2.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_liste_plantes .= "LEFT JOIN est_de_espece l3 INNER JOIN espece e ON e.CODEESPECE = l3.CODEESPECE ON l3.CODETAXON = t.CODETAXON AND l3.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_liste_plantes .= "LEFT JOIN est_de_sous_espece l33 INNER JOIN sous_espece sesp ON sesp.CODESOUSESPECE = l33.CODESOUSESPECE ON l33.CODETAXON = t.CODETAXON AND l33.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_liste_plantes .= "LEFT JOIN a_comme_variete l4 INNER JOIN variete v ON v.CODEVARIETE = l4.CODEVARIETE ON l4.CODETAXON = t.CODETAXON AND l4.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_liste_plantes .= "WHERE lp.CODELISTE = ".$_GET['id']." ";
	
	$result_recuperation_liste_plantes = mysql_query($query_recuperation_liste_plantes, $connexion) or logError("RECUPERATION liste plantes-".$query_recuperation_liste_plantes."-".mysql_error());
	
	while($tab_recuperation_liste_plantes = mysql_fetch_assoc($result_recuperation_liste_plantes)){
		
		$liste['nom'] = $tab_recuperation_liste_plantes['nom'];
		$liste['sachet'] = $tab_recuperation_liste_plantes['idSachet'];
		$liste['semis'] = $tab_recuperation_liste_plantes['idSemis'];
		$liste['touffe'] = $tab_recuperation_liste_plantes['idTouffe'];
		$liste['refSachet'] = $tab_recuperation_liste_plantes['refSachet'];
		$liste['refSemis'] = $tab_recuperation_liste_plantes['refSemis'];
		$liste['refTouffe'] = $tab_recuperation_liste_plantes['refTouffe'];
		$liste['taxon']['id'] = $tab_recuperation_liste_plantes['idTaxon'];
		$liste['taxon']['famille'] = $tab_recuperation_liste_plantes['famille'];
		$liste['taxon']['genre'] = $tab_recuperation_liste_plantes['genre'];
		$liste['taxon']['espece'] = $tab_recuperation_liste_plantes['espece'];
		$liste['taxon']['variete'] = $tab_recuperation_liste_plantes['variete'];
			
	}
	
	mysql_free_result($result_recuperation_liste_plantes);
	
	
?>
<h3>Détail d'une liste de plantes</h3>
<table class="fiche" style="width: 100%;">
	<tr>
		<td>
			<b><?php //echo htmlentities(Capitalise(liste['nom']), ENT_NOQUOTES, "UTF-8"); ?> </b>
		</td>
	</tr>
	<tr>
		<th>SACHET(s) DE GRAINES</th>
	</tr>
	<tr>
		<td>
		<?php 
			if(count($liste['sachet']) > 0){
				echo "<ul>";
				foreach($liste as $sachet){
					if (isset($sachet['idSachet'])){
						echo "<li>";
						echo "<a href=\"popup.php?p=gestion/sachetgraines_affiche_popup&amp;id=".$sachet['taxon']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\"><b>".htmlentities(capitalise($sachet['taxon']['famille']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($sachet['taxon']['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities($sachet['taxon']['espece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities($sachet['taxon']['sousespece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($sachet['taxon']['variete']), ENT_NOQUOTES, "UTF-8")."</b></a>";
						if(!is_null($sachet['refSachet'])){
							echo " (n° ent. ".htmlentities($sachet['refSachet'], ENT_NOQUOTES, "UTF-8").")";
						}
						echo "</a>";
						echo "</li>";
					}
				}
				unset($sachet);
				echo "</ul>";
			}else{
				echo "<i>aucun</i>";
			}
		?>
		</td>
	</tr>
	<tr>
		<th>SEMIS</th>
	</tr>
	<tr>
		<td>
		<?php 
			if(count($liste['semis']) > 0){
				echo "<ul>";
				foreach($liste as $semis){
					if (isset($semis)){
						echo "<li>";
						echo "<a href=\"popup.php?p=gestion/semisgraines_affiche_popup&amp;id=".$semis['taxon']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\"><b>".htmlentities(capitalise($semis['taxon']['famille']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($semis['taxon']['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities($semis['taxon']['espece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities($semis['taxon']['sousespece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($semis['taxon']['variete']), ENT_NOQUOTES, "UTF-8")."</b></a>";
						if(!is_null($semis['refSemis'])){
							echo " (n° ent. ".htmlentities($semis['refSemis'], ENT_NOQUOTES, "UTF-8").")";
						}
						echo "</a>";
						echo "</li>";
					}
				}
				unset($semis);
				echo "</ul>";
			}else{
				echo "<i>aucun</i>";
			}
		?>
		</td>
	</tr>
	<tr>
		<th>TOUFFE(s)</th>
	</tr>
	<tr>
		<td>
		<?php 
			if(count($liste['touffe']) > 0){
				echo "<ul>";
				foreach($liste as $touffe){
					if (isset($touffe)){
						echo "<li>";
						echo "<a href=\"popup.php?p=gestion/touffegraines_affiche_popup&amp;id=".$touffe['taxon']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\"><b>".htmlentities(capitalise($touffe['taxon']['famille']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($touffe['taxon']['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities($touffe['taxon']['espece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities($touffe['taxon']['sousespece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($touffe['taxon']['variete']), ENT_NOQUOTES, "UTF-8")."</b></a>";
						if(!is_null($touffe['refTouffe'])){
							echo " (n° ent. ".htmlentities($touffe['refTouffe'], ENT_NOQUOTES, "UTF-8").")";
						}
						echo "</a>";
						echo "</li>";
					}
				}
				unset($touffe);
				echo "</ul>";
			}else{
				echo "<i>aucune</i>";
			}
		?>
		</td>
	</tr>
</table>
<?php
//------------------------------------------------------------ Accès refusé à la page

			}else{
				afficherStatut("Le liste de plantes n'existe pas", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
			}

		}else{
			afficherStatut("Le liste de plantes n'est pas ou mal indiqué", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
		}

	}else{
		if(isset($_SESSION)){
			include("pages/401.htm");
		}
	}

?>